package dch.persistent.admin.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;

import dch.db.DataBaseDao;
import dch.db.DataBaseImpl;
import dch.entity.Types;
import dch.persistent.admin.dao.TypesDao;

public class TypesDaoImpl implements TypesDao {

	private Connection conn=null;
	private PreparedStatement pstmt=null;
	private ResultSet rs=null;
	
	List<Types> typeList=null;
	Types type=null;
	
	Logger logger=Logger.getLogger(TypesDaoImpl.class);
	
	private DataBaseDao db=new DataBaseImpl();
	
	public TypesDaoImpl(){
		conn=db.getConnection();
	}
	
	@Override
	public List<Types> queryByFather(int father) {
		typeList=new ArrayList<Types>();
		try{
			pstmt=conn.prepareStatement("select typeid,typename,islock from types where father=?");
			pstmt.setInt(1,father);
			rs=pstmt.executeQuery();
			while(rs.next()){
				type=new Types();
				type.setIslock(rs.getString("islock"));
				type.setTypeid(rs.getInt("typeid"));
				type.setTypename(rs.getString("typename"));
				
				typeList.add(type);
			}
		}catch(Exception e){
			e.printStackTrace();
		}
		return typeList;
	}

	@Override
	public int getChildrenCount(int father) {
		int count=0;
		try{
			pstmt=conn.prepareStatement("select count(typeid) from types where father=?");
			pstmt.setInt(1,father);
			rs=pstmt.executeQuery();
			if(rs.next()){
				count=rs.getInt(1);
			}
		}catch(Exception e){
			e.printStackTrace();
		}
		return count;
	}

	@Override
	public Types insert(Types type, int fatherId) {
		String fathers=null;
		try{
			pstmt=conn.prepareStatement("select distinct fathers from types where father=?");
			pstmt.setInt(1,fatherId);
			
			rs=pstmt.executeQuery();
			if(rs.next()){
				fathers=rs.getString("fathers");
			}
			pstmt=conn.prepareStatement("insert into types(typename,father,fathers) values(?,?,?)");
			pstmt.setString(1,type.getTypename());
			pstmt.setInt(2,fatherId);
			pstmt.setString(3,fathers);
			pstmt.executeUpdate();
			
			pstmt=conn.prepareStatement("select typeid,typename from types where typename=? and father=?");
			pstmt.setString(1,type.getTypename());
			pstmt.setInt(2,fatherId);
			rs=pstmt.executeQuery();
			if(rs.next()){
				type=new Types();
				type.setTypeid(rs.getInt("typeid"));
				type.setTypename(rs.getString("typename"));
				
			}
			
		}catch(Exception e){
			e.printStackTrace();
		}
		return type;
	}
	
	public void delete(int id){
		try{
			pstmt=conn.prepareStatement("delete from types where typeid=?");
			pstmt.setInt(1,id);
			pstmt.executeUpdate();
		}catch(Exception e){
			e.printStackTrace();
		}
	}
	
	public void update(Types type,int id){
		try{
			pstmt=conn.prepareStatement("update types set typename=? where typeid=?");
			pstmt.setString(1,type.getTypename());
			pstmt.setInt(2,id);
			pstmt.executeUpdate();
		}catch(Exception e){
			e.printStackTrace();
		}
	}

	@Override
	public void close() {
		try{
			if(null!=rs)
				rs.close();
			if(null!=pstmt)
				pstmt.close();
			if(null!=conn)
				conn.close();
		}catch(Exception e){
			e.printStackTrace();
		}
		
	}
}
